package com.example.gitpart03;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class ThirdPage extends AppCompatActivity {

    private int quantity = 1;
    private TextView quantityText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third_page);

        // 获取从上个页面传递过来的产品信息
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String productName = extras.getString("productName");
            String productPrice = extras.getString("productPrice");
            int productImageResId = extras.getInt("productImageResId");

            // 根据获取到的信息更新页面显示
            ImageView productImage = findViewById(R.id.product_image);
            TextView productNameText = findViewById(R.id.product_name);
            TextView productPriceText = findViewById(R.id.product_price);

            productImage.setImageResource(productImageResId);
            productNameText.setText(productName);
            productPriceText.setText(productPrice);
        }

        quantityText = findViewById(R.id.quantity_text);
        ImageView subNum = findViewById(R.id.sub_num);
        ImageView addNum = findViewById(R.id.add_num);

        subNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (quantity > 1) {
                    quantity--;
                    quantityText.setText(String.valueOf(quantity));
                }
            }
        });

        addNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quantity++;
                quantityText.setText(String.valueOf(quantity));
            }
        });

        // 评价部分
        RecyclerView recyclerView = findViewById(R.id.option_recy);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<PingJiaItem> pingJiaList = new ArrayList<>();
        pingJiaList.add(new PingJiaItem("用户1", "口感细腻，非常好喝！", "该用户觉得商品很赞！"));
        pingJiaList.add(new PingJiaItem("用户2", "味道不错，很新鲜！", "该用户觉得商品很棒！"));
        pingJiaList.add(new PingJiaItem("用户3", "包装精美，值得购买！", "该用户觉得商品很满意！"));
//适配器

        PingJiaAdapter adapter = new PingJiaAdapter(this, pingJiaList);
        recyclerView.setAdapter(adapter);

        // 处理立即购买按钮点击事件
        Button buyNowButton = findViewById(R.id.buy_now_button);
        buyNowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ThirdPage.this, MainActivity.class);
                intent.putExtra("productName", getIntent().getStringExtra("productName"));
                intent.putExtra("productImageResId", getIntent().getIntExtra("productImageResId", 0));
                intent.putExtra("showDingDanFragment", true);
                startActivity(intent);
            }
        });
    }
}